﻿using System;
using System.Linq;
using System.Windows;
using System.Windows.Input;

namespace NumberGuesser
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        Random rand = new Random();
        int userGuess;
        int compGuess;
        int turns = 5;
        int games = 0;
        int correct = 0;
        
        public MainWindow()
        {
            InitializeComponent();
            MessageBox.Show("Try and guess the same number that the computer guesses!");
            MessageBox.Show("Input 0 if you would like to quit the game.");
            compGuess = rand.Next(1, 20);
            compGuess = rand.Next(1, 20);
            turnsRemaining.Text = turns.ToString();
            gamesPlayed.Text = games.ToString();
            correctNumber.Text = correct.ToString();
        }

        public void Button1Click(object sender, RoutedEventArgs e)
        {
            GameBody();
        }

        private void InputNumberKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                GameBody(); 
            }
        }

        private void GameBody()
        {
            turnsRemaining.Text = turns.ToString();
            gamesPlayed.Text = games.ToString();
            correctNumber.Text = correct.ToString();
            tooHigh.Text = "";
            tooLow.Text = "";
            string input = inputNumber.Text;
            if (inputNumber.Text == "")
            {
                MessageBox.Show("That is an invalid entry please input a valid number.");
            }
            else
            {
                if (int.TryParse(input, out userGuess) == false)
                {
                    MessageBox.Show("Please input a valid number.");
                    inputNumber.Text = "";
                }
                else
                {
                    userGuess = int.Parse(inputNumber.Text);

                    if (userGuess == 0)
                    {
                        MessageBox.Show("Thanks for playing!");
                        this.Close();
                    }
                    else if (userGuess == compGuess)
                    {
                        MessageBox.Show("You guessed the correct number! You have earned more turns!");
                        turns = 5;
                        games++;
                        correct++;
                        turnsRemaining.Text = turns.ToString();
                        gamesPlayed.Text = games.ToString();
                        correctNumber.Text = correct.ToString();
                        tooHigh.Text = "";
                        tooLow.Text = "";
                        inputNumber.Text = "";
                        compGuess = rand.Next(1, 20);
                    }
                    else if (userGuess < compGuess && userGuess >= 1)
                    {
                        tooLow.Text = "That guess was too low!";
                        turns--;
                        turnsRemaining.Text = turns.ToString();
                        correctNumber.Text = correct.ToString();
                        inputNumber.Text = "";
                        if (turns == 0)
                        {
                            MessageBox.Show("You ran out of turns! Try Again!");
                            turns = 5;
                            games++;
                            turnsRemaining.Text = turns.ToString();
                            gamesPlayed.Text = games.ToString();
                            tooHigh.Text = "";
                            tooLow.Text = "";
                            inputNumber.Text = "";
                            compGuess = rand.Next(1, 20);
                        }
                    }
                    else if (userGuess > compGuess && userGuess <= 20)
                    {
                        tooHigh.Text = "That guess was too high!";
                        turns--;
                        turnsRemaining.Text = turns.ToString();
                        correctNumber.Text = correct.ToString();
                        inputNumber.Text = "";
                        if (turns == 0)
                        {
                            MessageBox.Show("You ran out of turns! Try Again!");
                            turns = 5;
                            games++;
                            turnsRemaining.Text = turns.ToString();
                            gamesPlayed.Text = games.ToString();
                            tooHigh.Text = "";
                            tooLow.Text = "";
                            inputNumber.Text = "";
                            compGuess = rand.Next(1, 20);
                        }
                    }
                    else
                    {
                        MessageBox.Show("That entry is invalid. Please input a valid selection.");
                        inputNumber.Text = "";
                    }
                }
            }
        }
    }
}
